import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/match/detail/zq_match_lineup_logic.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/card.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/ads_label.dart';

class ZqSuspendView extends StatefulWidget {
  const ZqSuspendView({super.key});

  @override
  State<ZqSuspendView> createState() => _ZqSuspendViewState();
}

class _ZqSuspendViewState extends State<ZqSuspendView> {
  final logic = Get.put(ZqMatchLineupLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ZqMatchLineupLogic>(builder: (_) {
      return ListView(padding: EdgeInsets.zero, children: [
        Container(height: 8),
        const AdsLabel(
          adsTag: "match_zq_伤禁",
          tapType: 'ggw_bsxq_sj',
          margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
        ),
        Block(
            cardStyle: const CardStyle(
                padding: EdgeInsets.only(top: 16, bottom: 24),
                margin: EdgeInsets.symmetric(horizontal: 8)),
            child: _suspend()),
        Container(height: 180)
      ]);
    });
  }

  Widget _suspend() {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Padding(
        padding: const EdgeInsets.only(left: 16),
        child: Row(
          children: [
            Image.asset(
                width: 24, Utils.getImgPath("match_lineup_suspend.png")),
            Container(width: 2),
            QsText('伤病及禁赛球员',
                fontSize: 16, color: Colours.text, fontWeight: FontWeight.w500),
          ],
        ),
      ),
      const SizedBox(height: 10),
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Flexible(child: _suspendList(isHome: true)),
          const SizedBox(width: 8),
          Flexible(child: _suspendList(isHome: false))
        ],
      )
    ]);
  }

  Widget _suspendList({required bool isHome}) {
    return Column(
      children: [
        Container(
          padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
          child: Row(
            children: [
              CachedNetworkImage(
                  width: 26,
                  height: 26,
                  placeholder: (context, url) => Styles.placeholderIcon(),
                  errorWidget: (
                    context,
                    url,
                    error,
                  ) =>
                      Image.asset(Utils.getImgPath("zq_placeholder.png")),
                  imageUrl: isHome
                      ? logic.detail.info?.baseInfo?.homeLogo ?? ""
                      : logic.detail.info?.baseInfo?.guestLogo ?? ""),
              Container(width: 12),
              Expanded(
                child: QsText(
                    isHome
                        ? logic.detail.info?.baseInfo?.homeName ?? ""
                        : logic.detail.info?.baseInfo?.guestName ?? "",
                    fontWeight: FontWeight.w500,
                    maxLines: 2),
              )
            ],
          ),
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16),
          child: (isHome
                  ? (logic.nowData?[1]?.homeSuspend?.noValue == true)
                  : (logic.nowData?[1]?.guestSuspend?.noValue == true))
              ? const Padding(
                  padding: EdgeInsets.symmetric(vertical: 18),
                  child: QsText('暂无数据', fontSize: 11, color: Colours.grey66))
              : Column(
                  children: List.generate(
                      isHome == true
                          ? logic.nowData![1]!.homeSuspend!.length
                          : logic.nowData![1]!.guestSuspend!.length,
                      (index) => _suspendPlayer(isHome, index)),
                ),
        ),
      ],
    );
  }

  Widget _suspendPlayer(bool isHome, int index) {
    final player = isHome
        ? logic.nowData![1]!.homeSuspend![index]
        : logic.nowData![1]!.guestSuspend![index];
    var reasonImage = '';
    if (player.kind == 0 || player.kind == 1 || player.kind == 2) {
      reasonImage = 'icon_jinsai.png';
    } else if (player.kind != 99) {
      reasonImage = 'icon_shangbing.png';
    }
    return Container(
      padding: const EdgeInsets.only(bottom: 14, top: 8),
      child: Row(children: [
        Container(
            width: 26,
            height: 26,
            alignment: Alignment.center,
            decoration: BoxDecoration(
                shape: BoxShape.circle,
                border: Border.all(
                    width: 1.5,
                    color: isHome ? Colours.homeColorRed : Colours.blue2766D6),
                color: isHome ? Colours.homeColorRed : Colours.blue2766D6),
            child: ClipOval(
              child: CachedNetworkImage(
                width: 26,
                height: 26,
                fit: BoxFit.cover,
                imageUrl: player.photo ?? "",
                placeholder: (context, url) => Styles.placeholderIcon(),
                errorWidget: (context, url, error) => Container(
                  alignment: Alignment.center,
                  child: QsText('${player.number}',
                      color: Colours.white,
                      fontWeight: FontWeight.w500,
                      fontFamily: "Din"),
                ),
              ),
            )),
        const SizedBox(width: 11),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Flexible(
                    fit: FlexFit.loose,
                    child: QsText((player.nameChs) ?? '',
                        maxLines: 1, fontSize: 14, fontWeight: FontWeight.w500),
                  ),
                  const SizedBox(width: 4),
                  if (reasonImage.isNotEmpty)
                    Image.asset(width: 12, Utils.getImgPath(reasonImage))
                ],
              ),
              QsText('${player.position ?? ''} ${player.reason ?? ''}',
                  fontSize: 11, color: Colours.grey66)
            ],
          ),
        )
      ]),
    );
  }
}
